using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace BizTalkCrossReferenceManager.UI
{
    public partial class ApplicationXRefIDsForm : Form
    {
        private string _XRefType;

        public ApplicationXRefIDsForm()
        {
            InitializeComponent();
        }

        private void ApplicationXRefIDsForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'crossReferenceDataDataSet.xref_IDXRef' table. You can move, or remove it, as needed.
            this.xref_IDXRefTableAdapter.Fill(this.crossReferenceDataDataSet.xref_IDXRef);            
        }

        private void QueryDataButton_Click(object sender, EventArgs e)
        {            
            this._XRefType = XRefComboBox.Text;

            DataSet ds = Data.CrossTab.GetCrossTabData(this._XRefType);
            if(ds.Tables.Count > 0)
                IDXRefDataGridView.DataSource = ds.Tables[0];

            foreach (DataGridViewColumn col in IDXRefDataGridView.Columns)
            {
                if (col.Index > 0)
                    col.ReadOnly = false;
            }
            foreach (DataGridViewRow row in IDXRefDataGridView.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (cell.ColumnIndex > 0)
                        cell.ReadOnly = false;
                }
            }

        }

       

        private void IDXRefDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {            
            if (e.ColumnIndex > 0)
            {
                string applicationInstance = IDXRefDataGridView.Columns[e.ColumnIndex].HeaderText;
                string common = (string)IDXRefDataGridView.Rows[e.RowIndex].Cells[0].Value;
                string appId;
                if (IDXRefDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == DBNull.Value)
                    appId = string.Empty;
                else
                    appId = (string)IDXRefDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;

                if (appId.Length > 255)
                {
                    MessageBox.Show("The length for the application id is greater than 255 characters.  This is too long");                    
                    return;
                }

                //MessageBox.Show(string.Format("App: {0}, Common:{1}, App Id:{2}", applicationInstance, common, appId));
                
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BizTalkCrossReferenceManager.Properties.Settings.CrossReferenceDataConnectionString"].ConnectionString))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand("UpdateApplicationXRefId", conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@IdXRef", this._XRefType);
                        cmd.Parameters.AddWithValue("@ApplicationName", applicationInstance);
                        cmd.Parameters.AddWithValue("@CommonIdDescription", common);
                        cmd.Parameters.AddWithValue("@ApplicationId", appId);
                        cmd.ExecuteNonQuery();                        
                    }
                    conn.Close();
                }
            }
        }

        private void IDXRefDataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            
        }

        private void IDXRefDataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.ColumnIndex == 0)
                e.Cancel = true;
        }
    }
}